
#ONOMA:LEANDROS TZANAKIS-ARNAOUTAKIS
#AEM:985
#PROVLIMA VANDERMORE

#VIVLIOTHIKEES
from numpy import zeros,array,product, \
                     diagonal,dot
from gaussElimin import*
from numpy import*

def vandermode(v):
    n = len(v)
    a = zeros((n,n))
    for j in range(n):
        a[:,j] = v**(n-j-1)
    return a

v = array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0])
b = array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
a = vandermode(v)
aOrig = a.copy()    # Save original matrix
bOrig = b.copy()    # and the constant vector

x = gaussElimin(a,b)           #klish synarthsshs fia epilush gauss


det = product(diagonal(a))     #ypologismos orizousas


print 'x =\n',x                 #ektypwsi lushs

print '\ndet =',det              #ektypwsi orizousas


print '\nCheck result: [a]{x} - b =\n', \
      dot(aOrig,x) - bOrig

y=linalg.norm(a)                        #calculate to norm(a)=sqrt(dot(a,a))

b = array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0],\
          [0.0,1.0,0.0,0.0,0.0,0.0],\
          [0.0,0.0,1.0,0.0,0.0,0.0],\
          [0.0,0.0,0.0,1.0,0.0,0.0],\
          [0.0,0.0,0.0,0.0,1.0,0.0],\
          [0.0,0.0,0.0,0.0,0.0,1.0]])       #kanoume ton b iso me ton tautotiko
                                            #b=I
  
#synarthsh metratophs tou a se LU  
def LUdecomp(a):
    n = len(a)
    for k in range(0,n-1):
        for i in range(k+1,n):
            if a[i,k] != 0.0:
                lam = a [i,k]/a[k,k]
                a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
                a[i,k] = lam
    return a

a=LUdecomp(a)


#LUSH SYSTHMATOS a*z=b=I gia eyresh antistrofou
def LUsolve(a,b):
    n = len(a)
    for k in range(1,n):
        b[k] = b[k] - dot(a[k,0:k],b[0:k])  
    for k in range(n-1,-1,-1):
        b[k] = (b[k] - dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
    return b

z=LUsolve(a,b)              #z=antistrofos tou x


print 'z =\n',z         

k=linalg.norm(z)                    #calculate norm tou z=sqrt(dot(z,z))

#numerical condition=|a|*|antistrofo tou a|=>|a|*|z|

cond=y*z                    #numerical number=deikths katastashs

print 'conditional number =\n',cond    #If this number is close to unity, the matrix is well-conditioned!



raw_input("\nPress return to exit")
